// 工位状态管理模块

class StatusManager {
    constructor() {
        this.defaultStatuses = [
            { id: 1, name: '工作中', color: '#28a745', description: '正在专注工作' },
            { id: 2, name: '会议中', color: '#17a2b8', description: '正在参加视频或现场会议' },
            { id: 3, name: '休息中', color: '#ffc107', description: '暂时离开，稍后回来' },
            { id: 4, name: '免打扰', color: '#dc3545', description: '请勿打扰，专注工作时间' }
        ];
        
        this.currentStatus = this.defaultStatuses[0];
        this.customStatuses = [];
        this.loadStatuses();
    }
    
    // 获取所有状态（包括默认和自定义）
    getAllStatuses() {
        return [...this.defaultStatuses, ...this.customStatuses];
    }
    
    // 获取当前状态
    getCurrentStatus() {
        return this.currentStatus;
    }
    
    // 设置当前状态
    setCurrentStatus(status) {
        this.currentStatus = status;
        this.saveCurrentStatus();
        return this.currentStatus;
    }
    
    // 切换到下一个状态
    toggleNextStatus() {
        const allStatuses = this.getAllStatuses();
        const currentIndex = allStatuses.findIndex(status => status.id === this.currentStatus.id);
        const nextIndex = (currentIndex + 1) % allStatuses.length;
        this.currentStatus = allStatuses[nextIndex];
        this.saveCurrentStatus();
        return this.currentStatus;
    }
    
    // 添加自定义状态
    addCustomStatus(name, color, description = '') {
        const newId = Date.now(); // 使用时间戳作为唯一ID
        const newStatus = {
            id: newId,
            name: name,
            color: color,
            description: description
        };
        
        this.customStatuses.push(newStatus);
        this.saveCustomStatuses();
        return newStatus;
    }
    
    // 删除自定义状态
    removeCustomStatus(id) {
        const index = this.customStatuses.findIndex(status => status.id === id);
        if (index > -1) {
            this.customStatuses.splice(index, 1);
            this.saveCustomStatuses();
            
            // 如果删除的是当前状态，则切换到默认状态
            if (this.currentStatus.id === id) {
                this.currentStatus = this.defaultStatuses[0];
                this.saveCurrentStatus();
            }
            
            return true;
        }
        return false;
    }
    
    // 更新自定义状态
    updateCustomStatus(id, name, color, description) {
        const status = this.customStatuses.find(s => s.id === id);
        if (status) {
            status.name = name;
            status.color = color;
            status.description = description;
            this.saveCustomStatuses();
            return status;
        }
        return null;
    }
    
    // 保存自定义状态到本地存储
    saveCustomStatuses() {
        try {
            localStorage.setItem('customStatuses', JSON.stringify(this.customStatuses));
        } catch (error) {
            console.error('保存自定义状态失败:', error);
        }
    }
    
    // 保存当前状态到本地存储
    saveCurrentStatus() {
        try {
            localStorage.setItem('currentStatus', JSON.stringify(this.currentStatus));
        } catch (error) {
            console.error('保存当前状态失败:', error);
        }
    }
    
    // 从本地存储加载状态
    loadStatuses() {
        try {
            const savedCustomStatuses = localStorage.getItem('customStatuses');
            if (savedCustomStatuses) {
                this.customStatuses = JSON.parse(savedCustomStatuses);
            }
            
            const savedCurrentStatus = localStorage.getItem('currentStatus');
            if (savedCurrentStatus) {
                this.currentStatus = JSON.parse(savedCurrentStatus);
            }
        } catch (error) {
            console.error('加载状态失败:', error);
        }
    }
}

// 导出单例实例
const statusManager = new StatusManager();
module.exports = statusManager;